import java.util.Arrays;

public class duipai {
    public static void main(String[] args) {
        int[] r={4,31,9,-4,91};
        sort(r);
        System.out.println(Arrays.toString(r));
    }
    public static void sort(int[] nums){
        for(int i=nums.length/2-1;i>=0;i--){
            adjust(nums,i,nums.length);
        }
        for(int i=nums.length-1;i>=0;i--){
            swap(nums,0,i);
            adjust(nums,0,i);
        }
    }
    public  static void adjust(int[] nums,int k,int length){
        int temp=nums[k];
        for(int i=2*k+1;i<length;i=i*2+1){
            if(i+1<=length-1&&nums[i+1]>nums[i]){
                i++;
            }
            if(nums[i]>temp){
                nums[k]=nums[i];
                k=i;
            }else {
              break;
            }
        }
        nums[k]=temp;
    }
    public static void swap(int[] nums,int i,int j){
        int temp=nums[i];
        nums[i]=nums[j];
        nums[j]=temp;
    }
}
